from common.selenium_co import sel_click, sel_send_keys
from selenium.webdriver.common.by import By
from settings import ENV

from time import sleep
import allure
from common.log import log


class Event:
    def event_login(self, driver, username, password):
        """登录事件:在登录页面进行登录的动作"""
        driver.get(ENV.URL)
        sel_click(driver, (By.XPATH, "//a[contains(text(),'登录')]"))
        sel_send_keys(driver, (By.XPATH, "//input[@placeholder='请输入用户名']"), username)
        sel_send_keys(driver, (By.XPATH, "//input[@placeholder='请输入密码']"), password)
        sel_click(driver, (By.XPATH, "//input[@value='登录']"))

    @staticmethod
    @allure.step('立即购买并提交订单')
    def event_submit_order(driver):
        """
        立即购买并点击提交订单
        :param driver:
        :return:
        """
        try:
            driver.get(ENV.URL + '/17/')  # 直接访问具体商品详情页：我们可以使用url拼接，这样打开的就是固定商品了
            # 打击打开商品详情页
            # driver.sel_click((By.XPATH, "//body/div[5]/div[2]/ul[1]/li[2]/a[1]/img[1]"))
            # 点击立即购买
            driver.sel_click((By.XPATH, "//a[@id='buy_now']"))
            # 点击去结算
            driver.sel_click((By.XPATH, "//a[contains(text(),'去结算')]"))
            # 点击提交订单
            driver.sel_click((By.XPATH, "//a[@id='order_btn']"))
            sleep(1)
        except Exception as e:
            log.error(f"立即购买并点击提交事件异常，异常信息为{e}")
            raise e

    @staticmethod
    @allure.step('登录')
    def event_login_po(driver, username, password):
        """
        po模式登录
        :param driver:
        :param username:
        :param password:
        :return:
        """
        try:
            driver.get(ENV.URL)
            driver.sel_click((By.XPATH, "//a[contains(text(),'登录')]"))
            driver.sel_send_keys((By.XPATH, "//input[@placeholder='请输入用户名']"), username)
            driver.sel_send_keys((By.XPATH, "//input[@placeholder='请输入密码']"), password)
            driver.sel_click((By.XPATH, "//input[@value='登录']"))
        except Exception as e:
            log.error(f"登录异常，为：{e}")
            raise e

    @staticmethod
    @allure.step('注册')
    def event_register(driver, user_name, pwd, cpwd, email):
        """
        注册事件
        :param driver:
        :param user_name:
        :param pwd:
        :param cpwd:
        :param email:
        :return:
        """
        try:
            driver.get(ENV.URL)
            driver.sel_click((By.XPATH, "//a[contains(text(),'注册')]"))
            driver.sel_send_keys((By.XPATH, "//input[@id='user_name']"), user_name)
            driver.sel_send_keys((By.XPATH, "//input[@id='pwd']"), pwd)
            driver.sel_send_keys((By.XPATH, "//input[@id='cpwd']"), cpwd)
            driver.sel_send_keys((By.XPATH, "//input[@id='email']"), email)
            driver.sel_click((By.XPATH, "//input[@value='注 册']"))
        except Exception as e:
            log.error(f'注册发生异常，{e}')
            raise e

    @staticmethod
    @allure.step('新增收货地址')
    def add_address(driver, ushou='李广', uphone='13599998888', uyoubian='412312', uaddress='南京路10号'):
        """
        新增收货地址
        :param driver:
        :param ushou:
        :param uphone:
        :param uyoubian:
        :param uaddress:
        :return:
        """
        try:
            driver.get(ENV.URL)
            driver.sel_click((By.XPATH, "//a[contains(text(),'用户中心')]"))
            driver.sel_click((By.XPATH, "//a[contains(text(),'· 收货地址')]"))
            driver.sel_send_keys((By.XPATH, "//input[@name='ushou']"), ushou)
            driver.sel_send_keys((By.XPATH, "//input[@name='uphone']"), uphone)
            driver.sel_send_keys((By.XPATH, "//input[@name='uyoubian']"), uyoubian)
            driver.sel_send_keys((By.XPATH, "//textarea[@name='uaddress']"), uaddress)
            driver.sel_click((By.XPATH, "//input[@value='修改地址']"))
        except Exception as e:
            log.error(f'新增收货地址出现异常：{e}')
            raise e

    @staticmethod
    @allure.step('从购物车去结算')
    def shopping_cart_go_to_settlement(driver):
        """
        从购物车去结算
        :param driver:
        :return:
        """
        try:
            # 访问购物车的路径
            driver.get(ENV.URL + 'cart/')
            # 点击去结算
            driver.sel_click((By.XPATH, "//a[contains(text(),'去结算')]"))
            # 点击提交订单
            driver.sel_click((By.XPATH, "//a[@id='order_btn']"))
        except Exception as e:
            log.error(f'从购物车结算发生异常：{e}')
            raise e

    @staticmethod
    @allure.step('主页搜索')
    def event_search_key(driver, input_text):
        """
        主页搜索功能
        :param driver:
        :param input_text: 输入文本
        :return:
        """
        try:
            # 直接进入首页
            driver.get(ENV.URL)
            driver.sel_send_keys((By.XPATH, "//input[@placeholder='搜索商品']"), input_text)
            driver.sel_click((By.XPATH, "//input[@value='搜索']"))
        except Exception as e:
            log.error(f'搜索发生异常：{e}')
            raise e
